package com.huluobo.a2203parkdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.webkit.JavascriptInterface
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.SPStaticUtils
import com.huluobo.a2203parkdemo.department.DepartmentActivity
import com.huluobo.a2203parkdemo.news.NewsActivity
import com.huluobo.a2203parkdemo.notice.AddNoticeActivity
import com.huluobo.a2203parkdemo.notice.NoticeActivity
import com.huluobo.a2203parkdemo.repair.RepairActivity
import com.huluobo.a2203parkdemo.signin.SignInActivity
import com.huluobo.a2203parkdemo.visitor.VisitorActivity
import com.huluobo.a2203parkdemo.widget.WebViewJavaScriptFunction
import com.huluobo.base.net.Const
import com.huluobo.a2203parkdemo.patrol.PatrolActivity
import com.huluobo.a2203parkdemo.repair.AddRepairActivity
import com.tencent.smtt.sdk.WebView

class MainActivity : AppCompatActivity() {
    private lateinit var url: String
    private lateinit var listener: MainWebViewJavaScriptFun
    private lateinit var main_x5_webview: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //主页面网址
        url = SPStaticUtils.getString(Const.PARAM_HOME)
        initWebView()

        findViewById<ImageView>(R.id.header_back).visibility = View.GONE

    }

    private fun initWebView() {
        listener = MainWebViewJavaScriptFun()
        main_x5_webview = findViewById(R.id.main_x5view)
        main_x5_webview.loadUrl(url)
        main_x5_webview.view.overScrollMode = View.OVER_SCROLL_ALWAYS
        main_x5_webview.addJavascriptInterface(listener, "androidinfo")
    }

    fun start(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        startActivity(intent)
    }


    inner class MainWebViewJavaScriptFun : WebViewJavaScriptFunction {
        override fun onJsFunctionCalled(tag: String) {
        }

        @JavascriptInterface
        fun androidsign(id: String) {
            //打开签到activity
            start(SignInActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddculture(id: String) {
            //打开新闻activity
            start(NewsActivity::class.java)
        }

        //注册要监听的js事件
        @JavascriptInterface
        fun androidparking(id: String?) {
            //车位申请
            Log.i("TagA", "androidparking")
        }

        @JavascriptInterface
        fun androidapply(id: String?) {
            //我的申请
            Log.i("TagA", "androidapply")
        }

        @JavascriptInterface
        fun androidvisit(id: String?) {
            //来访预约
            start(VisitorActivity::class.java)
        }

        @JavascriptInterface
        fun androidpatrol(id: String?) {
            //巡更管理
            start(PatrolActivity::class.java)
        }

        @JavascriptInterface
        fun androidnews(id: String?) {
            Log.i("TagA", "androidnews")
        }

        @JavascriptInterface
        fun androidnoticelist(id: String?) {
            Log.i("TagA", "androidnoticelist")
        }

        @JavascriptInterface
        fun androidrepair(id: String?) {
            //维修管理
            start(RepairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddrepair(id: String?) {
            //添加维修
            start(AddRepairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddnotice(id: String?) {
            //公告管理
            start(AddNoticeActivity::class.java)
        }

        @JavascriptInterface
        fun androidculture(id: String?) {
            //文化管理
            Log.i("TagA", "androidculture")
        }

        @JavascriptInterface
        fun androidnotice(id: String?) {
            //公告管理
            start(NoticeActivity::class.java)
        }

        @JavascriptInterface
        fun androidattendance(id: String?) {
            //考勤管理
            Log.i("TagA", "androidattendance")
        }

        @JavascriptInterface
        fun androidproperty(id: String?) {
            //物业审核
            Log.i("TagA", "androidproperty")
        }

        @JavascriptInterface
        fun androidcheckculture(id: String?) {
            //文化审核
            Log.i("TagA", "androidcheckculture")
        }

        @JavascriptInterface
        fun androidpeople(id: String?) {
            //人资管理
            start(DepartmentActivity::class.java)
        }
    }
}